package com.efamily.ewallet.client.common.widget;

import java.util.Arrays;
import java.util.List;

import com.efamily.ewallet.client.common.bean.CommentBean;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.BaseTreeLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.data.TreeLoader;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.StoreSorter;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGrid;
import com.extjs.gxt.ui.client.widget.treegrid.WidgetTreeGridCellRenderer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;

public abstract class CommentsTreeWidget extends Composite {
	private ContentPanel contentPanel = new ContentPanel();

	public CommentsTreeWidget() {
		contentPanel.setTitle("Comments");
		buildTree();
		initComponent(contentPanel);
	}

	public abstract void loadData(Object loadConfig,
			AsyncCallback<List<CommentBean>> callback);

	private void buildTree() {
		RpcProxy<List<CommentBean>> proxy = new RpcProxy<List<CommentBean>>() {
			@Override
			public void load(Object loadConfig,
					AsyncCallback<List<CommentBean>> callback) {
				loadData(loadConfig, callback);
			}
		};

		// loader
		final TreeLoader<CommentBean> loader = new BaseTreeLoader<CommentBean>(
				proxy) {
			@Override
			public boolean hasChildren(CommentBean parent) {
				return parent instanceof CommentBean;
			}
		};
		// trees store
		final TreeStore<CommentBean> store = new TreeStore<CommentBean>(loader);
		store.setStoreSorter(new StoreSorter<CommentBean>() {

			@Override
			public int compare(Store<CommentBean> store, CommentBean m1,
					CommentBean m2, String property) {
				boolean m1Comment = m1 instanceof CommentBean;
				boolean m2Comment = m2 instanceof CommentBean;

				if (m1Comment && !m2Comment) {
					return -1;
				} else if (!m1Comment && m2Comment) {
					return 1;
				}

				return super.compare(store, m1, m2, property);
			}
		});

		loader.load(null);

		ColumnConfig comment = new ColumnConfig("comment", "Comment", 100);
		comment.setRenderer(new WidgetTreeGridCellRenderer<CommentBean>() {

			@Override
			public Widget getWidget(CommentBean model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<CommentBean> store, Grid<CommentBean> grid) {
				return new CommentPanel(model);
			}

		});

		ColumnConfig author = new ColumnConfig("author", "Author", 100);
		ColumnConfig date = new ColumnConfig("date", "Date", 100);
		ColumnModel cm = new ColumnModel(Arrays.asList(comment, date, author));

		TreeGrid<CommentBean> tree = new TreeGrid<CommentBean>(store, cm);
		tree.setBorders(true);
		tree.setSize(400, 400);
		tree.setAutoExpandColumn("comment");
		tree.getTreeView().setBufferEnabled(false);
		tree.getTreeView().setRowHeight(26);
		tree.getStyle().setLeafIcon(IconHelper.createStyle("icon-music"));
		contentPanel.add(tree);
	}

	private class CommentPanel extends ContentPanel {
		private CommentBean commentBean;
		private Button replyBtn = new Button("Reply");
		private Button removeBtn = new Button("Remove");

		public CommentPanel(CommentBean commentBean) {
			this.commentBean = commentBean;
			initReplyButton();
			initRemoveButton();
			buildCommentPanel();
		}

		private void buildCommentPanel() {
			setHeaderVisible(false);
			setLayout(new FitLayout());
			 TextArea description = new TextArea();  
			     description.setPreventScrollbars(true);  
			     description.setHideLabel(true);
			     description.setValue(commentBean.getComment());
			     description.setHeight("90%");
			     description.setWidth("90%");
			     FormData formData = new FormData("-20"); 
			     add(description);  
			addButton(replyBtn);
			addButton(removeBtn);
		}

		private void initReplyButton() {
			replyBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
				@Override
				public void componentSelected(ButtonEvent ce) {
					/*add(new CommentForm(commentBean),
							new BorderLayoutData(LayoutRegion.SOUTH));*/
				}
			});
		}

		private void initRemoveButton() {
			removeBtn
					.addSelectionListener(new SelectionListener<ButtonEvent>() {
						@Override
						public void componentSelected(ButtonEvent ce) {
							removeFromParent();
						}
					});
		}

	}

	private class CommentForm extends Composite {
		private FormPanel formPanel = new FormPanel();
		private Button saveBtn = new Button();
		private CommentBean parent;

		public CommentForm() {
			buildCommentArea();
			initSaveBtn();
			initComponent(formPanel);
		}

		public CommentForm(CommentBean parent) {
			this();
			this.parent = parent;
		}

		private void buildCommentArea() {
			TextArea area = new TextArea();
			area.setSize(300, 200);
			formPanel.add(area);
			formPanel.addButton(saveBtn);
		}
		private void initSaveBtn(){
			saveBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
				@Override
				public void componentSelected(ButtonEvent ce) {
					//TODO save
				}
			});
		}
	}

}
